package com.sczmgk.hotline_pzh.utils;

import android.content.Context;

import com.google.gson.Gson;
import com.sczmgk.hotline_pzh.MyApp;
import com.sczmgk.hotline_pzh.callback.HttpFailCallback;
import com.sczmgk.hotline_pzh.callback.SimplePaserJsonCallback;
import com.sczmgk.hotline_pzh.callback.VerfyCallback;
import com.sczmgk.hotline_pzh.constants.Constants;
import com.sczmgk.hotline_pzh.db.impl.InformationTitleDaoImpl;
import com.sczmgk.hotline_pzh.entity.BaseEntity;
import com.sczmgk.hotline_pzh.entity.IndicatorBean;
import com.sczmgk.hotline_pzh.entity.IndicatorInfos;
import com.sczmgk.hotline_pzh.httputils.MyHttp;
import com.sczmgk.hotline_pzh.sp.CacheUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2016/12/23.
 */

public class SecurityCodeUtils {

    /**
     * 获取验证码
     */
    public static void getCode(final Context context, String phoneNumber, int codetype , int userid,  final VerfyCallback verfyCallback) {

        Map<String, Object> infos = new HashMap<>();

        infos.put("phone", phoneNumber);
        infos.put("codetype", codetype);
        infos.put("userid", userid);

        MyHttp.requestByCommon(Constants.BASE_URL + Constants.GET_SECURIT_CODE_URL, infos, MyHttp.POST, context, false, null, new SimplePaserJsonCallback() {

            @Override
            public void onVerifyIsNull() {
                if (verfyCallback != null) {
                    verfyCallback.onSuccess();
                }
            }

            @Override
            public void onVerifySuccess(String dataRaw, boolean isCache) {

            }

            @Override
            public void onErrorCodeError(BaseEntity.MyError mError) {
                super.onErrorCodeError(mError);
                if (verfyCallback != null) {
                    verfyCallback.onError(mError);
                }
            }
        });
    }

    public static void verfiCode(Context context, final String phone, String verfiCode, final VerfyCallback verfyCallback) {
        Map<String, Object> maps = new HashMap<>();
        maps.put("phone", phone);
        maps.put("verifycodes", verfiCode);
        MyHttp.requestByCommon(Constants.BASE_URL + Constants.POST_VERIFY_CODE_URL, maps, MyHttp.POST, context, false, null, new SimplePaserJsonCallback() {
            @Override
            public void onVerifyIsNull() {
                //跳转到输入密码的界面
                if (verfyCallback != null) {
                    verfyCallback.onSuccess();
                }
            }

            @Override
            public void onVerifySuccess(String dataRaw, boolean isCache) {

            }

            @Override
            public void onErrorCodeError(BaseEntity.MyError mError) {
                super.onErrorCodeError(mError);
                if (verfyCallback != null) {
                    verfyCallback.onError(mError);
                }
            }
        });
    }

    public static void getIndicatorData(final Context context, final VerfyCallback verfyCallback) {

        Map<String, Object> infos = new HashMap<>();
        infos.put("userid", CacheUtils.getIntData(context, Constants.LOGIN_UID, 0));
        infos.put("page", 1);
        infos.put("imei", MyApp.imei);

        MyHttp.requestByFailCallback(Constants.BASE_URL + Constants.GET_MY_COLLECTION_INDICATOR_URL, infos, MyHttp.GET, context, false, null, new SimplePaserJsonCallback() {

            @Override
            public void onVerifyIsNull() {

            }

            @Override
            public void onVerifySuccess(String dataRaw, boolean isCache) {
                try {
                    //解析指示器的Json数据
                    if (Tools.isEmpty(dataRaw)) {
                        MyLog.i("获取我的收藏指示器数据", "get indicator data json error");
                        return;
                    }
                    //解析并添加数据到集合中
                    Gson gson = new Gson();
                    IndicatorBean indicatorBean = gson.fromJson(dataRaw, IndicatorBean.class);
                    InformationTitleDaoImpl titleDao = InformationTitleDaoImpl.getIntance(context);
                    if(indicatorBean.datalist != null && indicatorBean.datalist.size()>0){
                          titleDao.deleteAll();
                        for (IndicatorInfos indicatorInfo : indicatorBean.datalist) {
                            //判断是否存在,不存在添加,存在则更新
                            if (titleDao.isExists(indicatorInfo.type)) {
                                IndicatorInfos old = titleDao.findByQuery(indicatorInfo.type);
                                indicatorInfo.id = old.id;
                                titleDao.updata(indicatorInfo);
                            } else {
                                titleDao.insert(indicatorInfo);
                            }
                        }
                        if(verfyCallback != null){

                            verfyCallback.onLoadIndicatorDataCallback(indicatorBean.datalist);
                        }
                    }

                } catch (Exception e) {

                    e.printStackTrace();
                }
            }


        }, new HttpFailCallback() {
            @Override
            public void onServiceErrorCallback() {
            }
        });

    }

}
